const { parse } = require('@babel/parser');
const traverse = require('@babel/traverse').default;

function findAssignmentInMethods(code) {
  // 解析代码为AST
  const ast = parse(code, {
    sourceType: 'module',
    plugins: ['objectRestSpread']
  });

  const results = [];

  // 遍历AST查找Vue组件中的methods选项
  traverse(ast, {
    ExportDefaultDeclaration(path) {
      const componentOptions = path.node.declaration;
      
      // 确保导出的是一个对象
      if (componentOptions.type === 'ObjectExpression') {
        // 查找methods属性
        const methodsProp = componentOptions.properties.find(
          prop => prop.key?.name === 'methods'
        );
        
        if (methodsProp && methodsProp.value.type === 'ObjectExpression') {
          // 遍历所有method方法
          methodsProp.value.properties.forEach(method => {
            // 使用path.traverse而不是直接调用traverse
            traverse(method, {
              AssignmentExpression(subPath) {
                const { node } = subPath;
                
                // 检查是否为 this.name = 555
                if (
                  node.left.type === 'MemberExpression' &&
                  node.left.object.type === 'ThisExpression' &&
                  node.left.property.type === 'Identifier' &&
                  node.left.property.name === 'name' &&
                  node.right.type === 'NumericLiteral' &&
                  node.right.value === 555
                ) {
                  // 记录匹配的赋值语句位置和信息
                  results.push({
                    methodName: method.key.name,
                    line: node.loc.start.line,
                    column: node.loc.start.column,
                    code: `${code.substring(node.start, node.end)};`
                  });
                }
              },
              // 重要：提供scope和parentPath
              enter(path) {
                path.setData('parentMethod', method.key.name);
              }
            }, path.scope, path); // 传递scope和parentPath
          });
        }
      }
    }
  });

  return results;
}

// 测试代码
const code = `
export default {
  computed: {
    oParamsT() {
      return { age: 999 }
    }
  },
  methods: {
    handleClick() {
      console.log('Button clicked');
    },
    getOutputModeInfo() {
      for (let i = 0; i < 100; i++) {
        if (i = 10) {
          console.log(i);
        } else {
          for (let indx = 0; indx < 10; indx++) {
            const arr = [1,2,3,5];
            arr.forEach((item, index) => {
              setTimeout(() => {
                this.getName('tt', {
                  success: () => {
                    this.name = 555;
                  }
                })
              }, 1000);
            })
          }
        }
      }
    }
  }
}
`;

const matches = findAssignmentInMethods(code);

if (matches.length > 0) {
  console.log(`找到 ${matches.length} 处匹配:`);
  matches.forEach((match, index) => {
    console.log(`匹配 ${index + 1}:`);
    console.log(`- 方法名: ${match.methodName}`);
    console.log(`- 位置: 第 ${match.line} 行, 第 ${match.column} 列`);
    console.log(`- 代码: ${match.code}`);
  });
} else {
  console.log('未找到匹配的赋值语句');
}